﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using ThiefRun.Behaviour;
using ThiefRun.States;

namespace ThiefRun.Character
{
    public class NPC : Character
    {
        #region Constants
        // Constants
        #endregion Constants

        #region Properties
        // Properties
        AIProperties aiProperties;

        //A Timer variable
        float timer = 0f;
        //The interval (300 milliseconds)
        float interval = 250f;
        
        #endregion Properties

        #region Computed Properties
        // Computed Properties

        Rectangle sourceRect;
        float angle = 0f;
        Vector2 origin;
        int currentFrame = 0;

        //Width of a single sprite image, not the whole Sprite Sheet
        int spriteWidth = 93;
        //Height of a single sprite image, not the whole Sprite Sheet
        int spriteHeight = 120;

        #endregion Computed Properties

        #region Methods
        // Methods
        public NPC(ThiefRunGame game, LevelState state, Texture2D texture, Vector2 position, Vector2 size, Vector2 direction, Vector2 velocity, Color colour,
            int level) : base(game, state, texture, position, size, direction, velocity, colour, level)
        {
            aiProperties = new AIProperties(Vector2.Zero, new Vector2(1F, 1F), new Vector2(1F, 1F), Vector2.Zero, new Vector2(1F, 1F), new Vector2(1F, 1F));
            this.size = (size/10) * 4;
        }

        public void Update(GameTime gameTime)
        {
            SteeringBehaviour.Seek(ref linear, ref state.Player.linear, aiProperties);

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                //Show the next frame
                currentFrame++;
                //Reset the timer
                timer = 0f;
            }
            //If we are on the last frame, reset back to the one before the first frame (because currentframe++ is called next so the next frame will be 1!)
            if (currentFrame == 2)
            {
                currentFrame = 0;
            }

            sourceRect = new Rectangle(currentFrame * spriteWidth, 480, spriteWidth, spriteHeight);
            origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
        }


        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(texture, linear.position, sourceRect, Color.White, angle, origin, 0.4f, SpriteEffects.None, 0);
            spriteBatch.End();
        }

        #endregion Methods
    }
}
